﻿using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BudgetSV.Common;
using BudgetData;
using System.Linq;

namespace BudgetSV.admin.manager
{
    public partial class depart_list : ManagePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("depart_list", DTEnums.ActionEnum.View.ToString()); //检查权限
                RptBind();
            }
        }

        //数据绑定
        private void RptBind()
        {
            IList<Organization> dt =GetLevel2(GetAdminInfo().org_id);

            this.rptList.DataSource = dt;
            this.rptList.DataBind();
        }

        //美化列表
        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Literal LitFirst = (Literal)e.Item.FindControl("LitFirst");
                HiddenField hidLayer = (HiddenField)e.Item.FindControl("hidLayer");

                Button btn = (Button)e.Item.FindControl("btnExpand");
                string LitImg1 = "<span class=\"folder-open\"></span>";
                if (btn != null)
                {
                    LitImg1 = "<span class=\"folder-open\" onclick=\"$('#" + btn.ClientID + "').trigger('click')\" ></span>";
                }
                string LitStyle = "<span style=\"display:inline-block;width:{0}px;\"></span>{1}{2}";
                string LitImg2 = "<span class=\"folder-line\"></span>";
                

                int classLayer = Convert.ToInt32(hidLayer.Value);
                if (classLayer == 1)
                {
                    LitFirst.Text = LitImg1;
                }
                else
                {
                    LitFirst.Text = string.Format(LitStyle, (classLayer - 2) * 24, LitImg2, LitImg1);
                }
            }
        }

        protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Expand")
            {
                var child = (Repeater)e.Item.FindControl("rptChildren");
                var div = e.Item.FindControl("shell");

                if (child.DataMember == "")
                {
                    long pid = Convert.ToInt64(e.CommandArgument);
                    var list = Mod.GetList<Organization>(c=>c.parent_id== pid).OrderBy(m=>m.sort_id).ToList();
                    child.DataSource = list;
                    child.DataBind();

                    child.DataMember = "E";
                }
                else
                {
                    if (child.DataMember == "E" || child.DataMember == "Ec")
                    {
                        div.Visible = false;
                        child.DataMember = "Eo";
                    }
                    else
                    {
                        div.Visible = true;
                        child.DataMember = "Ec";
                    }
                }
            }
        }

        //保存排序
        protected void btnSave_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("depart_list", DTEnums.ActionEnum.Edit.ToString()); //检查权限

            for (int i = 0; i < rptList.Items.Count; i++)
            {
                long id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                int sortId;
                if (!int.TryParse(((TextBox)rptList.Items[i].FindControl("txtSortId")).Text.Trim(), out sortId))
                {
                    sortId = 99;
                }
                Mod.Executed("update Organization set sort_id=" + sortId + " where id=" + id);
            }
            AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "保存部门排序"); //记录日志
            JscriptMsg("保存排序成功！", "depart_list.aspx", "Success", "parent.loadMenuTree(true)");
        }

        //删除导航
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("depart_list", DTEnums.ActionEnum.Delete.ToString()); //检查权限
            bool bDeleted = false;

            for (int i = 0; i < rptList.Items.Count; i++)
            {
                long id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    //有用户或者有下级机构就不能删
                    int iUserCount = Mod.GetList<Manager>(c=>c.org_id==id).Count();
                    int iChildrenCount = Mod.GetList<Organization>(c => c.parent_id == id).Count();
                    if (iUserCount > 0 || iChildrenCount > 0) continue;

                    bDeleted = true;
                    Mod.Executed("delete from Organization where id=" + id);
                }
            }

            if (bDeleted)
            {
                AddAdminLog(DTEnums.ActionEnum.Delete.ToString(), "删除部门信息"); //记录日志
                JscriptMsg("删除数据成功！", "depart_list.aspx", "Success", "parent.loadMenuTree(true)");
            }
            else
            {
                JscriptMsg("选中的机构下含有用户或下级机构，不能删除！", "depart_list.aspx", "Error");
            }
        }

        public string GetAddHtmlSr(int classlayer,int orgid) {
            if (classlayer <3)
            {
                return "<a href=\"depart_edit.aspx?action=Add&id=" + orgid + "\">添加子级</a>";
            }
            else {
                return "";//"<span style=\"color:#808080\">添加子级</span>";
            }
            
        }
        
    }
}